package com.guli.mall.pms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.constant.ProductEnum;
import com.guli.common.utils.QuickSort;
import com.guli.common.utils.R;
import com.guli.mall.pms.feign.AliOssFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.pms.mapper.SkuImagesMapper;
import com.guli.mall.pms.entity.SkuImagesEntity;
import com.guli.mall.pms.service.SkuImagesService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesMapper, SkuImagesEntity> implements SkuImagesService {

    @Resource
    AliOssFeignClient aliOssFeignClient;

    private static String imagePrefix;

    @Override
    public PageUtils queryPage(Integer currentPage, Integer size) {

        Page<SkuImagesEntity> page = new Page<>(currentPage, size);

        Page<SkuImagesEntity> resultPage = this.page(page);


        return new PageUtils(resultPage);
    }

    @Override
    public List<SkuImagesEntity> getImagesBySkuId(Long skuId) {

        QueryWrapper<SkuImagesEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("sku_id", skuId);

        List<SkuImagesEntity> list = this.list(wrapper);

        QuickSort.sort(list, (c1, c2) -> {

            if (c1.getDefaultImg() == ProductEnum.DefaultImage.YES.getCode()) {

                if (c2.getDefaultImg() == ProductEnum.DefaultImage.YES.getCode()) {
                    return c1.getImgSort() - c2.getImgSort(); // c1 c2 都是默认图片
                } else {
                    return 1; // 只有 c1 是默认图片
                }
            } else {
                if (c2.getDefaultImg() == ProductEnum.DefaultImage.YES.getCode()) {
                    return -1; // 只有 c2 是否认图片
                } else {
                    return c1.getImgSort() - c2.getImgSort(); // 都不是默认图片
                }

            }

        });

        list.forEach(img -> {
            img.setImgUrl(imagePrefix + img.getImgUrl());
        });
        return list;
    }

    Lock lock = new ReentrantLock();

    public String getImagePrefix() {

        if (StringUtils.isEmpty(imagePrefix)) {

            lock.lock();
            try {
                while (StringUtils.isEmpty(imagePrefix)){
                    afterPropertiesSet();
                }
            }finally {
                lock.unlock();
            }

        }

        return imagePrefix;
    }

    public void afterPropertiesSet() {
        R ossHost = aliOssFeignClient.getOssHost();

        imagePrefix = ossHost.get("host") + "/";

        log.info("阿里云OSS地址已初始化。。");
    }


}